package webapp.mvc.wechat_handlers;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutNewsMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import webapp.mvc.components.SiteProperties;
import webapp.mvc.controllers.HomeController;
import webapp.mvc.enums.UserGenders;
import webapp.mvc.exceptions.UserException;
import webapp.mvc.models.UserEntity;
import webapp.mvc.services.ICategoryService;
import webapp.mvc.services.IShopService;
import webapp.mvc.services.IUserService;

import java.util.Map;

public class WechatMyFavoritesHandler implements WxMpMessageHandler {
    private static final Logger LOG = LoggerFactory.getLogger(WechatMyFavoritesHandler.class);

    @Autowired
    private IUserService userService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IShopService shopService;
    @Autowired
    private SiteProperties siteProperties;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        String openId = wxMpXmlMessage.getFromUserName();
        UserEntity userEntity = userService.findUserByWechatOpenId(openId);
        if (userEntity == null) {
            try {
                userEntity = userService.createWechatUserBinding(openId, openId, UserGenders.PRIVATE, "");
            } catch (UserException e) {
                LOG.error("create wechat user binding error", e);
                return null;
            }
        }
        me.chanjar.weixin.mp.bean.outxmlbuilder.NewsBuilder builder = WxMpXmlOutMessage.NEWS()
                .toUser(wxMpXmlMessage.getFromUserName())
                .fromUser(wxMpXmlMessage.getToUserName());
        // FIXME: use once token to auto login
        WxMpXmlOutNewsMessage.Item article1 = new WxMpXmlOutNewsMessage.Item();
        article1.setUrl(siteProperties.getHost() + "/" + "?" + HomeController.WECHAT_AUTOLOGIN_KEY + "=" + openId + "#/my_favorites");
        article1.setPicUrl("http://images.newsmth.net/nForum/img/hotpic/ITExpress_1568963.jpg");
        article1.setDescription("收藏的商户");
        article1.setTitle("收藏的商户");
        builder.addArticle(article1);
        WxMpXmlOutNewsMessage.Item article2 = new WxMpXmlOutNewsMessage.Item();
        article2.setUrl(siteProperties.getHost() + "/" + "?" + HomeController.WECHAT_AUTOLOGIN_KEY + "=" + openId + "#/my_favorites");
        article2.setPicUrl(siteProperties.getDefaultWechatPictureUrl());
        article2.setDescription("收藏的商品");
        article2.setTitle("收藏的商品");
        builder.addArticle(article2);
        builder.build();
        return builder.build();
    }
}
